package com.medical_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical_backend.entity.Payment;
import com.medical_backend.entity.Result;
import com.medical_backend.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;


    //获取慢病信息列表
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        QueryWrapper<Payment> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(keyword)) {
            queryWrapper.lambda().like(Payment::getCardId, keyword).or().like(Payment::getInsuredName, keyword);
        }
        Page<Payment> pageInfo = paymentService.page(new Page<>(page, size),queryWrapper);
        return new Result("查询成功",pageInfo,200);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Payment payment){
        // 避免前端误传 id，手动置空
        payment.setId(null);

        boolean save = paymentService.save(payment);
        if (save) {
            return new Result("新增成功", payment, 200);
        } else {
            return new Result("新增失败", null, 500);
        }
    }

    @PostMapping("/edit")
    public Result update(@RequestBody Payment payment){
        paymentService.updateById(payment);
        return new Result("修改成功",null,200);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean isDeleted = paymentService.removeById(id);
        if (isDeleted) {
            return new Result("删除成功", null, 200);
        } else {
            return new Result("删除失败: 慢病政策不存在", null, 404);
        }
    }
}
